// sm2_final_test.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>

void print_hex(const char* label, const unsigned char* data, size_t len) {
    printf("%s: ", label);
    for (size_t i = 0; i < len; i++) {
        printf("%02x", data[i]);
    }
    printf("\n");
}

int main() {
    SM2_KEY key;
    unsigned char message[] = "Hello SM2 Final Test";
    unsigned char ciphertext[512];
    unsigned char decrypted[512];
    size_t ciphertext_len, decrypted_len;
    
    printf("=== SM2 Algorithm Test ===\n\n");
    
    // 1. 生成SM2密钥对
    printf("1. Generating SM2 key pair using GmSSL API...\n");
    if (sm2_key_generate(&key) != 1) {
        printf("❌ SM2 key generation failed\n");
        return 1;
    }
    printf("✅ SM2 key pair generated successfully\n\n");
    
    // 2. 使用gmssl命令行生成密钥文件
    printf("2. Creating key files for GmSSL verification...\n");
    system("gmssl sm2keygen -pass 1234 -out sm2_private.pem -pubout sm2_public.pem");
    printf("✅ Key files created:\n");
    printf("   - sm2_private.pem (with password: 1234)\n");
    printf("   - sm2_public.pem\n\n");
    
    // 3. 保存测试消息
    printf("3. Creating test files...\n");
    FILE* fp = fopen("test_message.txt", "w");
    if (fp) {
        fwrite(message, 1, strlen((char*)message), fp);
        fclose(fp);
    }
    printf("✅ Test message: '%s'\n", message);
    printf("✅ Message length: %zu bytes\n\n", strlen((char*)message));
    
    // 4. 使用我们的程序加密
    printf("4. Encrypting with our program...\n");
    if (sm2_encrypt(&key, message, strlen((char*)message), ciphertext, &ciphertext_len) != 1) {
        printf("❌ SM2 encryption failed\n");
        return 1;
    }
    printf("✅ Encryption successful\n");
    printf("✅ Ciphertext length: %zu bytes\n", ciphertext_len);
    
    // 保存密文
    fp = fopen("our_cipher.bin", "wb");
    if (fp) {
        fwrite(ciphertext, 1, ciphertext_len, fp);
        fclose(fp);
        printf("✅ Ciphertext saved to: our_cipher.bin\n");
    }
    printf("\n");
    
    // 5. 使用我们的程序解密
    printf("5. Decrypting with our program...\n");
    if (sm2_decrypt(&key, ciphertext, ciphertext_len, decrypted, &decrypted_len) != 1) {
        printf("❌ SM2 decryption failed\n");
        return 1;
    }
    decrypted[decrypted_len] = '\0';
    printf("✅ Decryption successful\n");
    printf("✅ Decrypted message: '%s'\n", decrypted);
    
    // 保存解密结果
    fp = fopen("our_decrypted.txt", "w");
    if (fp) {
        fwrite(decrypted, 1, decrypted_len, fp);
        fclose(fp);
        printf("✅ Decrypted text saved to: our_decrypted.txt\n\n");
    }
    
    // 6. 验证我们的实现
    printf("6. Verifying our implementation...\n");
    if (memcmp(message, decrypted, strlen((char*)message)) == 0) {
        printf("✅ Our SM2 implementation: PASS\n");
    } else {
        printf("❌ Our SM2 implementation: FAIL\n");
        return 1;
    }
    printf("\n");
    
    // 7. 生成GmSSL验证指令
    printf("7. GmSSL Verification Commands:\n");
    printf("================================\n");
    printf("Run these commands to verify with GmSSL:\n\n");
    
    printf("A) Encrypt with GmSSL:\n");
    printf("   gmssl sm2encrypt -pubin -inkey sm2_public.pem -in test_message.txt -out gmssl_cipher.bin\n\n");
    
    printf("B) Decrypt GmSSL's ciphertext:\n");
    printf("   gmssl sm2decrypt -pass 1234 -inkey sm2_private.pem -in gmssl_cipher.bin\n\n");
    
    printf("C) Decrypt OUR ciphertext with GmSSL:\n");
    printf("   gmssl sm2decrypt -pass 1234 -inkey sm2_private.pem -in our_cipher.bin\n\n");
    
    printf("================================\n");
    printf("✅ SM2 algorithm test completed!\n");
    printf("✅ Ready for GmSSL command line verification!\n");
    
    return 0;
}
